import pymysql
from django.http import HttpResponse
from django.shortcuts import render

# Create your views here.
# 渲染登录首页
from user.models import User

def login_view(request):
    return render(request, 'html/login.html')

# 处理登陆请求
def login_check_view(request):
    # 接收用户登录信息
    username = request.POST.get('username', '')
    password = request.POST.get('password', '')
    # 判断用户信息并做出响应
    user = User.objects.filter(username=username,password=password)
    if user:
        return HttpResponse('登录成功！！！')
    return HttpResponse('登陆失败！！！')

def register_view(request):
    return render(request, 'html/register.html')


def register_check_view(request):
    # 获取用户注册信息
    username = request.POST.get('username', '')
    phonenumber = request.POST.get('phonenumber', '')
    password1 = request.POST.get('password1', '')
    password2 = request.POST.get('password2', '')

    # 判断注册用户信息
    if password1 == password2:
        # 创建对象 + 插入数据库
        user = User(username=username, phonenumber=phonenumber, password=password1)
        user.save()
        return HttpResponse('注册成功！！！')
    return HttpResponse("密码不一致！！注册失败！！！")


def t_user_view(request):
    # 接收表数据
    userlist = User.objects.all()
    return render(request,'html/show_t_user.html',{'users':userlist})